package com.shopping.controller.front;

import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.shopping.commons.controller.BaseController;
import com.shopping.commons.response.MessageResp;
import com.shopping.commons.service.CookieService;
import com.shopping.commons.util.CommonCode;
import com.shopping.commons.util.JsonUtil;
import com.shopping.model.User;
import com.shopping.param.UserParam;
import com.shopping.response.UserResp;
import com.shopping.service.IAddressService;
import com.shopping.service.IOrderService;
import com.shopping.service.IUserService;
import com.shopping.service.impl.ShopCarService;

/**
 * 用户的   前台的   Controller
 * @author Administrator
 *
 */
@Controller
@RequestMapping("front/user") 
public class FrontUserController extends BaseController{

	@Autowired
	private IUserService userService;
	
	@Autowired
	private IAddressService addressService;
	
	@Autowired
	private IOrderService orderService;
	
	@Autowired
	private ShopCarService shopCarService;
	
	/**
	 * 登录
	 * @param request
	 * @param param
	 * @return
	 */
	@RequestMapping("login")
	@ResponseBody  ///这个是吧MessageResp转成  json 的注解   普遍就是   对应  ajax 的请求    
	public MessageResp login(HttpServletRequest request,UserParam param){
		HttpSession session=request.getSession();
		MessageResp resp=new MessageResp();
		resp=userService.login(param);
		if(CommonCode.SUCCESS.equals(resp.getCode())){
			//session  不像application   session是与浏览器绑定的
			//，因此一个浏览器进行一次访问，只会是一个用户
			// application  服务器中所有的session公用的，  userid，username来确定用户
			session.setAttribute("userInfo", resp.getObj());
			//TODO: 维护购物车数据
			session.setAttribute("shopCarCount", shopCarService.getCount(((UserResp)resp.getObj()).getUserid()));
		}
		return resp;
	}
	
	/**
	 * 用户注册
	 * @param param
	 * @return
	 */
	@RequestMapping("register")
	@ResponseBody
	public MessageResp register(User param) {
		MessageResp resp=new MessageResp();
		resp=userService.register(param);
		return resp;
	}
	
	/**
	 * 用户登出
	 * @param param
	 * @return
	 */
	@RequestMapping("exit")
	@ResponseBody
	public MessageResp exit(HttpServletRequest request) {
		MessageResp resp=new MessageResp();
		try {
			request.getSession().removeAttribute("userInfo");
			request.getSession().removeAttribute("shopCarCount");
		} catch (Exception e) {
			resp.setCode(CommonCode.FAIL);
			resp.setMsg("登出错误");
			return resp;
		}
		resp.setCode(CommonCode.SUCCESS);
		resp.setMsg("登出错误");
		return resp;
	}
	
	/**
	 * 进入用户中心页面，
	 * @param param
	 * @return
	 */
	@RequestMapping("toUserInfo")
	@ResponseBody
	public ModelAndView toUserInfo(HttpServletRequest request) {
		ModelAndView mv=null;
		UserResp retUser=this.getUser(request);
		if(retUser==null){
			return new ModelAndView("redirect:../../views/front/index.jsp");
		}
		mv=new ModelAndView("views/front/login/userInfo");
		//查用户对应的收货地址
		mv.addObject("addressList",addressService.findAddressByUser(retUser.getUserid()));
		//查用户对应的订单信息
		mv.addObject("orderDetails",orderService.findOrderAndDetails(retUser.getUserid()));
		return mv;
	}
	
	/**
	 * 进入首页
	 * @param param
	 * @return
	 */
	@RequestMapping("toIndex")
	@ResponseBody
	public ModelAndView toIndex(HttpServletRequest request) {
		ModelAndView mv=new ModelAndView("views/front/index");
		UserResp retUser=this.getUser(request);
		int shopCarCount=0;
		if(retUser==null){
			//从cookie读
			Cookie c=CookieService.getCookieByName(request, "shopCar");
			if(c==null){
				shopCarCount=0;
			}else{
				Map<String,Integer> shopCarMap=JsonUtil.jsonToObject(c.getValue(), Map.class);
				if(shopCarMap==null){
					shopCarCount=0;
				}else{
					shopCarCount=shopCarMap.size();
				}
			}
		}
		mv.addObject("shopCarCount",shopCarCount);
		return mv;
	}

}
